//
//  HomeBottomLeftView.swift
//  ExtremeHealth
//
//  Created by Zhongkai Li 李忠凯 on 2018/9/29.
//  Copyright © 2018 GanDongKeJi. All rights reserved.
//

import SwiftFontName

class HomeBottomLeftView: HomeCardView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        titleLabel.text = "睡眠"
        setUpViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func tap() {
        MainCoordinator.shared.sleep()
    }

    // swiftlint:disable function_body_length
    private func setUpViews() {
        let recordView = SleepRecordView()
        recordView.record = "aaaaabbacabccacbacabccc"
        recordView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(recordView)
        NSLayoutConstraint.activate([
            recordView.leftAnchor.constraint(equalTo: leftAnchor, constant: 20),
            recordView.rightAnchor.constraint(equalTo: rightAnchor, constant: -20),
            recordView.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 0.4),
            NSLayoutConstraint(item: recordView, attribute: .centerY, relatedBy: .equal,
                               toItem: self, attribute: .centerY, multiplier: 0.8, constant: 0)
            ])

        let stateLabel = PaddingLabel()
        stateLabel.insets = UIEdgeInsets(top: 2, left: 3, bottom: 2, right: 3)
        stateLabel.text = "良好"
        stateLabel.font = UIFont(name: FontName.PingFangSCMedium, size: 12)
        stateLabel.textColor = .white
        stateLabel.backgroundColor = UIColor("#72D7FF")
        stateLabel.layer.cornerRadius = 4
        stateLabel.layer.masksToBounds = true
        stateLabel.translatesAutoresizingMaskIntoConstraints = false
        addSubview(stateLabel)
        NSLayoutConstraint.activate([
            stateLabel.rightAnchor.constraint(equalTo: rightAnchor, constant: -20),
            stateLabel.centerYAnchor.constraint(equalTo: titleLabel.centerYAnchor)
            ])

        let scoreLabel = UILabel()
        scoreLabel.text = "79分"
        scoreLabel.font = UIFont(name: FontName.PingFangSCLight, size: 14)
        scoreLabel.textColor = UIColor("#333333")
        scoreLabel.translatesAutoresizingMaskIntoConstraints = false
        addSubview(scoreLabel)
        NSLayoutConstraint.activate([
            scoreLabel.rightAnchor.constraint(equalTo: stateLabel.leftAnchor, constant: -12),
            scoreLabel.centerYAnchor.constraint(equalTo: titleLabel.centerYAnchor)
            ])

        let startTimeLabel = UILabel()
        startTimeLabel.text = "11:30"
        startTimeLabel.font = UIFont(name: FontName.PingFangSCLight, size: 12)
        startTimeLabel.textColor = UIColor("#333333")
        startTimeLabel.translatesAutoresizingMaskIntoConstraints = false
        addSubview(startTimeLabel)
        NSLayoutConstraint.activate([
            startTimeLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: 20),
            startTimeLabel.topAnchor.constraint(equalTo: recordView.bottomAnchor, constant: 5)
            ])

        let endTimeLabel = UILabel()
        endTimeLabel.text = "11:23"
        endTimeLabel.font = UIFont(name: FontName.PingFangSCLight, size: 12)
        endTimeLabel.textColor = UIColor("#333333")
        endTimeLabel.translatesAutoresizingMaskIntoConstraints = false
        addSubview(endTimeLabel)
        NSLayoutConstraint.activate([
            endTimeLabel.rightAnchor.constraint(equalTo: rightAnchor, constant: -20),
            endTimeLabel.topAnchor.constraint(equalTo: recordView.bottomAnchor, constant: 5)
            ])

        let leftView = SleepCardStackView()
        leftView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(leftView)
        NSLayoutConstraint.activate([
            leftView.leftAnchor.constraint(equalTo: leftAnchor, constant: 20),
            leftView.topAnchor.constraint(equalTo: startTimeLabel.bottomAnchor, constant: 6)
            ])

        let centerView = SleepCardStackView()
        centerView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(centerView)
        NSLayoutConstraint.activate([
            centerView.centerXAnchor.constraint(equalTo: centerXAnchor),
            centerView.topAnchor.constraint(equalTo: startTimeLabel.bottomAnchor, constant: 6)
            ])

        let rightView = SleepCardStackView()
        rightView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(rightView)
        NSLayoutConstraint.activate([
            rightView.rightAnchor.constraint(equalTo: rightAnchor, constant: -20),
            rightView.topAnchor.constraint(equalTo: startTimeLabel.bottomAnchor, constant: 6)
            ])
    }
    // swiftlint:enable function_body_length
}
